﻿using Microsoft.Reporting.WinForms;
using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using 际海环境软件.Class;

namespace PLC
{
    public partial class reportWindow : Form
    {
        DataTable origialTable;


        private string vtemp;
        private string vhumi;
        private string van;
        private string vvoc;
        private string vh2s;
        private string vpm25;
        

        private string fan;
        private string ftemp;
        private string fhumi;
        private string fh2s;
        private string fvoc;      
        private string fpm25;

        private string sname;
        private string stime;
        private string sresult="当前数据";

        private int res;

        
       
        public reportWindow()
        {
            InitializeComponent();
        }

        private void reportWindow_Load(object sender, EventArgs e)
        {
            //Form1.Logger.Info("***************报表页面****************");
            DateTime dateTime;                             
            dataGridView1.AutoSizeColumnsMode = System.Windows.Forms.DataGridViewAutoSizeColumnsMode.Fill;          
            dataGridView1.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
            dataGridView1.RowsDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;

            dataGridView2.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
            dataGridView2.RowsDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
            dataGridView2.AllowUserToAddRows = false;
            dataGridView2.AllowUserToDeleteRows = false;
            dataGridView2.ReadOnly = true;
            dataGridView2.AutoSizeColumnsMode = System.Windows.Forms.DataGridViewAutoSizeColumnsMode.Fill;

            dateTime = DateTime.Now;
            dateTimePicker1.MaxDate = dateTime;
            dateTimePicker2.MaxDate = dateTime;
            dateTimePicker1.Value = dateTime.AddDays(-1);
            dateTimePicker2.Value = dateTime;

            GetHistoryTab(1);
            GetRefTab();
            update_reportviewer();          
        }
        private void dataGridView2_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                van = dataGridView2.Rows[dataGridView2.CurrentRow.Index].Cells[0].Value.ToString();
                vtemp = dataGridView2.Rows[dataGridView2.CurrentRow.Index].Cells[1].Value.ToString();
                vhumi = dataGridView2.Rows[dataGridView2.CurrentRow.Index].Cells[2].Value.ToString();
                vh2s = dataGridView2.Rows[dataGridView2.CurrentRow.Index].Cells[3].Value.ToString();
                vvoc = dataGridView2.Rows[dataGridView2.CurrentRow.Index].Cells[4].Value.ToString();
                vpm25 = dataGridView2.Rows[dataGridView2.CurrentRow.Index].Cells[5].Value.ToString();               
                            
                stime = dataGridView2.Rows[dataGridView2.CurrentRow.Index].Cells[7].Value.ToString();
                res = int.Parse(origialTable.Rows[dataGridView2.CurrentRow.Index]["result"].ToString());
                if (radioButton1.Checked) sname = "整容室";
                else sname = "火化间";
            }
            catch (Exception ex)
            {
                //Form1.Logger.Error("cellclick 错误 "+ex.ToString());
            }

            update_reportviewer();
        }
        public void update_reportviewer()
        {
            bool[] isOk = new bool[8];
            int disres = 0;
            ///----指定报表外部数据源
            try
            {
                DataTable table = new DataTable();
                table.Columns.Add("an_val", typeof(string));
                table.Columns.Add("temp_val", typeof(string));
                table.Columns.Add("humi_val", typeof(string));
                table.Columns.Add("h2s_val", typeof(string));
                table.Columns.Add("voc_val", typeof(string));
                table.Columns.Add("pm25_val", typeof(string));
                table.Columns.Add("an_ref", typeof(string));
                table.Columns.Add("temp_ref", typeof(string));
                table.Columns.Add("humi_ref", typeof(string));
                table.Columns.Add("h2s_ref", typeof(string));
                table.Columns.Add("voc_ref", typeof(string));
                table.Columns.Add("pm25_ref", typeof(string));
                table.Columns.Add("house_name", typeof(string));
                table.Columns.Add("sys_time", typeof(string));
                table.Columns.Add("result", typeof(string));
                table.Columns.Add("res", typeof(string));
                if (res == 0)
                {
                    disres = 0;
                    sresult = "经过数据采样对比，当前环境质量合格";                    
                }                
                else
                {
                    for (int i = 0; i < 6; i++)
                    {
                        int zy = 1<< i;
                        int hhh = res & zy;
                        isOk[i] = ((res & zy) == 0 ? false : true);

                    }
                    if (isOk[0] == true)
                    {
                        sresult += "氨气不合格！";
                        disres += 1;
                    }
                    if (isOk[1] == true)
                    {
                        sresult += "温度不合格！";
                        disres += 10;
                    }
                    if (isOk[2] == true)
                    {
                        sresult += "湿度不合格！";
                        disres += 100;
                    }
                    if (isOk[3] == true)
                    {
                        sresult += "硫化氢不合格！";
                        disres += 1000;
                    }
                    if (isOk[4] == true)
                    {
                        sresult += "VOC不合格！";
                        disres += 10000;
                    }
                    if (isOk[5] == true)
                    {
                        sresult += "PM2.5不合格！";
                        disres += 100000;
                    }
                    
                }
                table.Rows.Add(van,
                               vtemp,
                               vhumi,
                               vh2s,
                               vvoc,
                               vpm25,
                               fan,
                               ftemp,
                               fhumi,
                               fh2s,
                               fvoc,
                               fpm25,
                               sname,                           
                               stime,
                               sresult,
                               disres);
                disres = 0;

                ///---添加数据源  
                ReportDataSource rds = new ReportDataSource();
                rds.Name = "DataSet1";
                rds.Value = table;
                ///---向报表绑定数据源  
                this.reportViewer1.LocalReport.DataSources.Clear();//注，使用原文代码上测试，数据一直不显示，后来添加了这句代码，代码出现了。MS真坑。
                this.reportViewer1.LocalReport.DataSources.Add(rds);
                ///---向报表查看器指定显示的报表  
                this.reportViewer1.LocalReport.ReportPath = Application.StartupPath + @"\Report1.rdlc";
                sresult = "当前数据";
                this.reportViewer1.RefreshReport();
            }
            catch(Exception ex)
            {
                //Form1.Logger.Error("报表更新错误 "+ex.ToString());
            }          
        }
        private void GetHistoryTab(int id)
        {           
            DataTable dataTable;          
            try
            {
                dataTable=MySqlHelp.GetHistoryData(dateTimePicker1.Value,dateTimePicker2.Value,id);
                origialTable= MySqlHelp.GetHistoryOrigi(dateTimePicker1.Value, dateTimePicker2.Value,id);

                dataGridView2.DataSource = dataTable;
                dataGridView2.RowHeadersVisible = false;                                                         //删除第一列空白列
               
                bool[] isOk =new bool[6];
                int Rowi = 0;
                if (this.dataGridView2.Rows.Count > 0)
                {
                    foreach (DataRow Row in origialTable.Rows)
                    {
                        int res = int.Parse(Row["result"].ToString());
                        if (res != 0)
                        {
                            for (int i = 0; i < 6; i++)
                            {
                                int zy = 1 << i;
                                int hhh = res & zy;
                                isOk[i] = ((res & zy) == 0 ? false : true);

                            }
                            if (isOk[0] == true) dataGridView2.Rows[Rowi].Cells[0].Style.BackColor = Color.Red;
                            if (isOk[1] == true) dataGridView2.Rows[Rowi].Cells[1].Style.BackColor = Color.Red;
                            if (isOk[2] == true) dataGridView2.Rows[Rowi].Cells[2].Style.BackColor = Color.Red;
                            if (isOk[3] == true) dataGridView2.Rows[Rowi].Cells[3].Style.BackColor = Color.Red;
                            if (isOk[4] == true) dataGridView2.Rows[Rowi].Cells[4].Style.BackColor = Color.Red;
                            if (isOk[5] == true) dataGridView2.Rows[Rowi].Cells[5].Style.BackColor = Color.Red;
                        }
                        Rowi++;
                    }

                    this.dataGridView2.FirstDisplayedScrollingRowIndex = 0;              //定位至最新一行显示 
                                                                      
                    dataGridView2.Rows[0].Selected = true;

                    van =   dataGridView2.Rows[0].Cells[0].Value.ToString();
                    vtemp = dataGridView2.Rows[0].Cells[1].Value.ToString();
                    vhumi = dataGridView2.Rows[0].Cells[2].Value.ToString();
                    vh2s =  dataGridView2.Rows[0].Cells[3].Value.ToString();
                    vvoc =  dataGridView2.Rows[0].Cells[4].Value.ToString();
                    vpm25 = dataGridView2.Rows[0].Cells[5].Value.ToString();
                                      
                    stime = dataGridView2.Rows[0].Cells[7].Value.ToString();                
                    res = int.Parse(origialTable.Rows[0]["result"].ToString());
                    if (id == 1) sname = "整容室";
                    else sname = "火化间";
                }
            }
            catch(Exception ex)
            {
                //Form1.Logger.Error("获取历史数据错误或导入dateview错误 "+ex.ToString());
            }          
        }
        private void GetRefTab()
        {
            DataTable dataTable;
            
            dataTable = MySqlHelp.GetRefTable();

            try
            {
                dataGridView1.DataSource = dataTable;

                fan = dataGridView1.Rows[0].Cells[0].Value.ToString();
                ftemp = dataGridView1.Rows[0].Cells[1].Value.ToString();
                fhumi = dataGridView1.Rows[0].Cells[2].Value.ToString();
                fh2s = dataGridView1.Rows[0].Cells[3].Value.ToString();
                fvoc = dataGridView1.Rows[0].Cells[4].Value.ToString();               
                fpm25 = dataGridView1.Rows[0].Cells[5].Value.ToString();
                
            }
            catch(Exception ex)
            {
               // Form1.Logger.Error("获取参考数据错误或导入dateview错误 " + ex.ToString());
            }          
        }
        private void dateTimePicker1_MouseDown(object sender, MouseEventArgs e)
        {
            DateTime dateTime=DateTime.Now;
            try
            {
                dateTimePicker1.MaxDate = dateTime;              
                dateTimePicker1.Value = dateTime.AddDays(-1);               
            }
            catch(Exception  ex)
            {
                //Form1.Logger.Error("timepicker1 错误 "+ex.ToString());
            }
           
        }
        private void dateTimePicker2_MouseDown(object sender, MouseEventArgs e)
        {
            DateTime dateTime = DateTime.Now;
            try
            {               
                dateTimePicker2.MaxDate = dateTime;               
                dateTimePicker2.Value = dateTime;
            }
            catch (Exception ex)
            {
                //Form1.Logger.Error("timepicker2 错误 " + ex.ToString());
            }
        }
        private void button1_Click(object sender, EventArgs e)
        {
            if(radioButton1.Checked) GetHistoryTab(1);
            else GetHistoryTab(2);
            update_reportviewer();
        }
        private void reportWindow_FormClosed(object sender, FormClosedEventArgs e)
        {
            //Form1.Logger.Info("***************报表页面退出****************");
        }

        private void button2_Click(object sender, EventArgs e)
        {
            DateTime dateTime = DateTime.Now;
            try
            {
                dateTimePicker2.MaxDate = dateTime;
                dateTimePicker2.Value = dateTime;
                if (radioButton1.Checked) GetHistoryTab(1);
                else GetHistoryTab(2);
                update_reportviewer();
            }
            catch (Exception ex)
            {
                //Form1.Logger.Error("刷新 错误 " + ex.ToString());
            }
        }
    }
}
